using System;
using System.Collections.Generic;
using System.Text;

namespace BAFIRCSharp
{
    public enum IRCChannelUserStatus
    {
        Op,
        Halfop,
        Voice,
        Normal
    }

    public struct IRCChannelUser
    {
        public string Nickname;
        public string Username;
        public string Hostname;
        public string Realname;

        public IRCChannelUserStatus Status;

        public IRCChannelUser(SenderInf Usr)
        {
            this.Nickname = Usr.Nickname;
            this.Username = Usr.Username;
            this.Hostname = Usr.Hostname;
            this.Realname = "not looked up yet - IRCChannel.cs line 29";

            Status = IRCChannelUserStatus.Normal;
        }

        public IRCChannelUser(string Nick, IRCChannelUserStatus Status)
        {
            this.Nickname = Nick;
            this.Username = "not looked up yet - IRCChannel.cs line 29";
            this.Hostname = "not looked up yet - IRCChannel.cs line 29";
            this.Realname = "not looked up yet - IRCChannel.cs line 29";

            this.Status = Status;
        }
    }

    public struct TopicInf
    {
        public string Topic;
        public string TopicBy;
        public DateTime TopicWhen;
    }

    public class IRCChannel // Add channel logging and mode tracking and such.
    {
        // Information about channel
        public bool   Joined;
        public string ChannelName;
        public TopicInf Topic;
        public string Modes;
        public List<IRCChannelUser> Users;
        public bool UserListComplete = false;

        // Management stuff
        public List<ChatMessage> ChatBuffer;

        // Constructors
        public IRCChannel()
        {
            this.ChannelName = "";
            this.Topic = new TopicInf();
            this.Modes = "";
            this.Users = new List<IRCChannelUser>();
            ChatBuffer = new List<ChatMessage>();
        }

        public IRCChannel(string ChannelName)
        {
            this.ChannelName = ChannelName;
            this.Topic = new TopicInf();
            this.Modes = "";
            this.Users = new List<IRCChannelUser>();
            ChatBuffer = new List<ChatMessage>();
        }

        public IRCChannel(string ChannelName, TopicInf Topic)
        {
            this.ChannelName = ChannelName;
            this.Topic = Topic;
            this.Modes = "";
            this.Users = new List<IRCChannelUser>();
            ChatBuffer = new List<ChatMessage>();
        }

        public IRCChannel(string ChannelName, TopicInf Topic, string Modes)
        {
            this.ChannelName = ChannelName;
            this.Topic = Topic;
            this.Modes = Modes;
            this.Users = new List<IRCChannelUser>();
            ChatBuffer = new List<ChatMessage>();
        }

        public IRCChannel(string ChannelName, TopicInf Topic, string Modes, List<IRCChannelUser> Users)
        {
            this.ChannelName = ChannelName;
            this.Topic = Topic;
            this.Modes = Modes;
            this.Users = Users;
            ChatBuffer = new List<ChatMessage>();
        }

        public void NewPrivMsg(SenderInf Who, string Message)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.PRIVMSG, DateTime.Now, Who, Message));
            Console.WriteLine("Chan  " + ChannelName + " " + DateTime.Now + " <" + Who.Nickname + "> " + Message);
        }

        public void NewNotice(SenderInf Who, string Message)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.NOTICE, DateTime.Now, Who, Message));
            Console.WriteLine("Chan  " + ChannelName + " " + DateTime.Now + " -" + Who.Nickname + "- " + Message);
        }

        public void NewTopic(SenderInf Who, string Topic)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.TOPIC, DateTime.Now, Who, Topic));
            this.Topic.Topic = Topic;
            this.Topic.TopicBy = Who.Nickname;
            this.Topic.TopicWhen = DateTime.Now;
        }

        public void NewMode(SenderInf Who, string Mode)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.MODE, DateTime.Now, Who, Mode));
        }

        public void NewJoin(SenderInf Who)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.JOIN, DateTime.Now, Who, ""));
            Users.Add(new IRCChannelUser(Who));
        }

        public void NewPart(SenderInf Who, string Reason)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.PART, DateTime.Now, Who, Reason));
        }

        public void NewKick(SenderInf Who, SenderInf ByWho, string Reason)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.KICK, DateTime.Now, ByWho, Who, Reason));
        }

        public void NewQuit(SenderInf Who, string Reason)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.QUIT, DateTime.Now, Who, Reason));
        }

        public void NewKill(SenderInf Who, SenderInf ByWho, string Reason)
        {
            ChatBuffer.Add(new ChatMessage(ChatMessage.ChatMessageType.KILL, DateTime.Now, Who, ByWho, Reason));
        }

        // Get buffer for showing on screen
        public List<ChatMessage> GetScreen(int NumMessages)
        {
            /*string[] ParamSplit = { "\r\n" };
            string[] Lines = ChatBuffer.Split(ParamSplit, StringSplitOptions.RemoveEmptyEntries);
            int Start = Lines.Length - NumLines;
            if (Start < 0)
                Start = 0;
            NumLines = Lines.Length - Start;

            string[] Ret = new string[NumLines];
            for (int i = 0; i < NumLines; ++i)
            {
                Ret[i] = Lines[Start + i];
            }

            return Ret;*/
            return null;
        }
    }
}
